﻿<%@ WebHandler Language="C#" Class="AjaxTable" %>

using System;
using System.Web;
using System.Data;
using System.Text;
using GXDXGY.App_Code;

public class AjaxTable : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        if (context.Request.Form["_type"] != null)
        {
            switch (context.Request.Form["_type"])
            {
                case "_DownPageAllPageNum": SendAllPageNum(context); break;
                case "_DownDQPageInfoArray": SendPageInfoArray(context); break;
                case "_DownDQPageInfoArray_all": SendAllPageInfoArray(context); break;
                case "_DataRowDel": SendDelIsTrue(context); break;
                default: break;
            } 
        }
    }

    /// <summary>
    /// 删除一条数据
    /// </summary>
    /// <param name="context"></param>
    private void SendDelIsTrue(HttpContext context)
    {
        string TableName = context.Request.Form["_TableName"];
        string TableKey = context.Request.Form["_TableKey"];
        string TableKeyVal = context.Request.Form["_TableKeyVal"];
        string SelectWhere = context.Request.Form["_SelectWhere"];
        string SqlStr = "";
        if (SelectWhere.Length != 0)
        {
            SqlStr = "delete from " + TableName + " where " + TableKey + "='" + TableKeyVal + "' and " + SelectWhere;
        }
        else
        {
            SqlStr = "delete from " + TableName + " where " + TableKey + "='" + TableKeyVal + "'";
        }
        if (new DBHelper().ExecSql(SqlStr))
        {
            context.Response.Write("true");
        }
        else
        {
            context.Response.Write("false"); 
        }
    }
    
    /// <summary>
    /// 发送当前表里所有信息(不分页)
    /// </summary>
    /// <param name="context"></param>
    private void SendAllPageInfoArray(HttpContext context)
    {
        string TableName = context.Request.Form["_TableName"];
        string FiledsStr = context.Request.Form["_FiledsStr"];
        string SelectWhere = context.Request.Form["_SelectWhere"];
        string SqlStr = "select " + FiledsStr + " from " + TableName;
        if (SelectWhere.Length != 0)
        {
            SqlStr += " where " + SelectWhere; 
        }
        DataSet ds = new DBHelper().GetDataSet(SqlStr);
        string ResStr = CreateJsonParameters(ds.Tables[0]);
        context.Response.Write(ResStr);
    }
    
    /// <summary>
    /// 发送当前条件下当前页的信息(数据)
    /// </summary>
    /// <param name="context"></param>
    private void SendPageInfoArray(HttpContext context)
    {
        string TableName = context.Request.Form["_TableName"];
        string TableKey = context.Request.Form["_TableKey"];
        string FiledsStr = context.Request.Form["_FiledsStr"];
        string SelectWhere = context.Request.Form["_SelectWhere"];
        string PageSize = context.Request.Form["_PageSize"];
        string PageNum = context.Request.Form["_PageNum"];
        int PageSizeInt=Convert.ToInt32(PageSize);
        int PageNumInt=Convert.ToInt32(PageNum);
        string SqlStr = "select top ";
        if (PageNum == "1")
        {
            SqlStr += PageSize + " " + FiledsStr + " from " + TableName;
            if (SelectWhere.Length != 0)
            {
                SqlStr += "  where " + SelectWhere;
            }
        }
        else
        {
            if (SelectWhere.Length != 0)
            {
                SqlStr += PageSize + " " + FiledsStr + " from " + TableName + " where (" + TableKey + " not in(select top " + ((PageNumInt - 1) * PageSizeInt).ToString() + " " + TableKey + " from " + TableName + " where " + SelectWhere + ")) and " + SelectWhere;
            }
            else
            {
                SqlStr += PageSize + " " + FiledsStr + " from " + TableName + " where " + TableKey + " not in(select top " + ((PageNumInt - 1) * PageSizeInt).ToString() + " " + TableKey + " from " + TableName + ")";
            }
        }
        DataSet ds = new DBHelper().GetDataSet(SqlStr);
        string resStr = CreateJsonParameters(ds.Tables[0]);
        context.Response.Write(resStr);
    }

    /// <summary>
    /// 发送到前台当前条件下表的页数
    /// </summary>
    /// <param name="context"></param>
    private void SendAllPageNum(HttpContext context)
    {
        string TableName = context.Request.Form["_TableName"];
        string SelectWhere = context.Request.Form["_SelectWhere"];
        string PageSize = context.Request.Form["_PageSize"];
        int PageSizeInt = Convert.ToInt32(PageSize);
        
        string SqlStr = "select count(*) from " + TableName;
        if (SelectWhere.Length != 0)
        {
            SqlStr += " where " + SelectWhere; 
        }
        int TableCountInt = new DBHelper().GetExexScalar(SqlStr);
        int PageCountInt = 0;
        if (TableCountInt % PageSizeInt == 0)
        {
            PageCountInt = TableCountInt / PageSizeInt;
        }
        else
        {
            PageCountInt = TableCountInt / PageSizeInt + 1; 
        }
        context.Response.Write(PageCountInt.ToString());
    }
    
    /// <summary>
    /// 将一个数据表转换成一个JSON字符串，在客户端可以直接转换成二维数组。
    /// </summary>
    /// <param name="source">需要转换的表。</param>
    /// <returns></returns>
    public static string DataTableToJson(DataTable source)
    {
        if (source.Rows.Count == 0)
            return "";
        StringBuilder sb = new StringBuilder("[");
        foreach (DataRow row in source.Rows)
        {
            sb.Append("[");
            for (int i = 0; i < source.Columns.Count; i++)
            {
                sb.Append('"' + row[i].ToString() + "\",");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append("],");
        }
        sb.Remove(sb.Length - 1, 1);
        sb.Append("]");
        return sb.ToString();
    }


    /// <summary>
    /// 反回JSON数据到前台
    /// </summary>
    /// <param name="dt">数据表</param>
    /// <returns>JSON字符串</returns>
    public string CreateJsonParameters(DataTable dt)
    {
        StringBuilder JsonString = new StringBuilder();
        //Exception Handling        
        if (dt != null && dt.Rows.Count > 0)
        {
            JsonString.Append("{ ");
            JsonString.Append("\"TableInfo\":[ ");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                JsonString.Append("{ ");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (j < dt.Columns.Count - 1)
                    {
                        JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                    }
                    else if (j == dt.Columns.Count - 1)
                    {
                        JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                    }
                }
                /**/
                /*end Of String*/
                if (i == dt.Rows.Count - 1)
                {
                    JsonString.Append("} ");
                }
                else
                {
                    JsonString.Append("}, ");
                }
            }
            JsonString.Append("]}");
            return JsonString.ToString();
        }
        else
        {
            return null;
        }
    }

    
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}